package com.luxsoft.siipap.em.dashboards;

import java.awt.Color;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Point;
import java.text.DecimalFormat;
import java.text.NumberFormat;

import org.jfree.chart.plot.dial.DialBackground;
import org.jfree.chart.plot.dial.DialCap;
import org.jfree.chart.plot.dial.DialPlot;
import org.jfree.chart.plot.dial.DialPointer;
import org.jfree.chart.plot.dial.DialTextAnnotation;
import org.jfree.chart.plot.dial.DialValueIndicator;
import org.jfree.chart.plot.dial.StandardDialFrame;
import org.jfree.chart.plot.dial.StandardDialRange;
import org.jfree.chart.plot.dial.StandardDialScale;
import org.jfree.data.general.ValueDataset;
import org.jfree.ui.GradientPaintTransformType;
import org.jfree.ui.StandardGradientPaintTransformer;

public class GraphFactory {
	
	/**
	 * Crea un {@link DialPlot} decorado en forma adecuada para dos {@link ValueDataset}
	 * 
	 * @param dataset1
	 * @param dataset2
	 * @return
	 */
	public static DialPlot createDialPlot(final ValueDataset dataset1,final ValueDataset dataset2,String valueTitle){
		
		// get data for diagrams
        DialPlot plot = new DialPlot();
        plot.setView(0.0, 0.0, 1.0, 1.0);
        plot.setDataset(0, dataset1);
        plot.setDataset(1, dataset2);
        
        DecimalFormat nf=(DecimalFormat)NumberFormat.getPercentInstance();
        nf.setMultiplier(1);
        
        StandardDialFrame dialFrame = new StandardDialFrame();
        dialFrame.setBackgroundPaint(Color.lightGray);
        dialFrame.setForegroundPaint(Color.darkGray);
        plot.setDialFrame(dialFrame);

        GradientPaint gp = new GradientPaint(new Point(),
                new Color(255, 255, 255), new Point(),
                new Color(170, 170, 220));
        DialBackground db = new DialBackground(gp);
        db.setGradientPaintTransformer(new StandardGradientPaintTransformer(
                GradientPaintTransformType.VERTICAL));
        plot.setBackground(db);

        DialTextAnnotation annotation1 = new DialTextAnnotation(valueTitle);
        annotation1.setFont(new Font("Dialog", Font.BOLD, 14));
        annotation1.setRadius(0.4);
        plot.addLayer(annotation1);
        

        DialValueIndicator dvi = new DialValueIndicator();
        dvi.setFont(new Font("Dialog", Font.PLAIN, 10));
        dvi.setOutlinePaint(Color.darkGray);
        dvi.setRadius(0.60);
        dvi.setAngle(-103.0);
        dvi.setNumberFormat(nf);
        plot.addLayer(dvi);

        DialValueIndicator dvi2 = new DialValueIndicator(1);
        dvi2.setFont(new Font("Dialog", Font.PLAIN, 10));
        dvi2.setOutlinePaint(Color.red);
        dvi2.setRadius(0.60);
        dvi2.setAngle(-77.0);
        dvi2.setNumberFormat(nf);
        plot.addLayer(dvi2);

        StandardDialScale scale = new StandardDialScale();
        scale.setTickRadius(0.88);
        scale.setTickLabelOffset(0.15);
        scale.setTickLabelFont(new Font("Dialog", Font.PLAIN, 14));
        scale.setTickLabelFormatter(nf);
        
        plot.addScale(0, scale);

        StandardDialScale scale2 = new StandardDialScale();
        scale2.setTickRadius(0.50);
        scale2.setTickLabelOffset(0.15);
        scale2.setTickLabelFont(new Font("Dialog", Font.PLAIN, 10));
        scale2.setMajorTickPaint(Color.red);
        scale2.setTickLabelFormatter(nf);
        plot.addScale(1, scale2);
        plot.mapDatasetToScale(1, 1);
        
        /**Agregamos los indicadores de Rangos **/
        
        StandardDialRange range1 = new StandardDialRange(0.0, 10.0, Color.GREEN);
        range1.setInnerRadius(0.62);
        range1.setOuterRadius(0.60);
        plot.addLayer(range1);

        StandardDialRange range2 = new StandardDialRange(11.0, 30.0,Color.YELLOW);
        range2.setInnerRadius(0.62);
        range2.setOuterRadius(0.60);
        plot.addLayer(range2);
        
        StandardDialRange range3 = new StandardDialRange(31.0, 100.0, Color.red);
        range3.setInnerRadius(.62);
        range3.setOuterRadius(0.60);        
        plot.addLayer(range3);

        DialPointer needle2 = new DialPointer.Pin(1);
        needle2.setRadius(0.55);
        plot.addLayer(needle2);

        DialPointer needle = new DialPointer.Pointer(0);
        plot.addLayer(needle);

        DialCap cap = new DialCap();
        cap.setRadius(0.10);
        plot.setCap(cap);
        return plot;
	}
	
	public static DialPlot createStandarDialPlot(final ValueDataset dataSet,final String valueTitle){
		
		// get data for diagrams
        DialPlot plot = new DialPlot();
        plot.setView(0.0, 0.0, 1.0, 1.0);
        plot.setDataset(dataSet);
        
        DecimalFormat nf=(DecimalFormat)NumberFormat.getPercentInstance();
        nf.setMultiplier(1);
        
        StandardDialFrame dialFrame = new StandardDialFrame();
        dialFrame.setBackgroundPaint(Color.lightGray);
        dialFrame.setForegroundPaint(Color.darkGray);
        plot.setDialFrame(dialFrame);

        GradientPaint gp = new GradientPaint(new Point(),
                new Color(255, 255, 255), new Point(),
                new Color(170, 170, 220));
        DialBackground db = new DialBackground(gp);
        db.setGradientPaintTransformer(new StandardGradientPaintTransformer(
                GradientPaintTransformType.VERTICAL));
        plot.setBackground(db);

        DialTextAnnotation annotation1 = new DialTextAnnotation(valueTitle);
        annotation1.setFont(new Font("Dialog", Font.BOLD, 14));
        annotation1.setRadius(0.4);

        plot.addLayer(annotation1);

        DialValueIndicator dvi = new DialValueIndicator();
        dvi.setNumberFormat(nf);
        plot.addLayer(dvi);

        StandardDialScale scale = new StandardDialScale();
        scale.setTickRadius(0.88);
        scale.setTickLabelOffset(0.15);
        scale.setTickLabelFont(new Font("Dialog", Font.PLAIN, 14));
        
        scale.setTickLabelFormatter(nf);
        plot.addScale(0, scale);

        StandardDialRange range = new StandardDialRange(31.0, 100.0, Color.red);
        range.setInnerRadius(0.54);
        range.setOuterRadius(0.55);
        plot.addLayer(range);

        StandardDialRange range2 = new StandardDialRange(11.0, 30.0,Color.YELLOW);
        range2.setInnerRadius(0.54);
        range2.setOuterRadius(0.55);
        plot.addLayer(range2);

        StandardDialRange range3 = new StandardDialRange(0.0, 10.0,Color.green);
        
        range3.setInnerRadius(0.54);
        range3.setOuterRadius(0.55);
        plot.addLayer(range3);

        DialPointer needle = new DialPointer.Pointer();
        plot.addLayer(needle);

        DialCap cap = new DialCap();
        cap.setRadius(0.10);
        plot.setCap(cap);
        
        return plot;
	}
	

}
